from math import acos, pi
from pyglet.gl import *

from awesum import *
from car import Car

class SceneTest(Scene):
	def __init__(self):
		super(SceneTest, self).__init__()
		self.car = None
		self.ballV = Vector3()
		self.yAng = 0.
	
	def load(self, game):
		
		game.cam.set(Vector3(-1, 1, 12), Vector3(1, 0, -5), Vector3(0, 1, 0))
		self.car = Car()
		self.carQ = Quaternion().setEulerDeg(Vector3())
		self.car.origin = Vector3(0, 0, 3)
		self.drawables.append(self.car)
		
		super(SceneTest, self).load(game)
		self.drawables[-1].gl.scaleV(Vector3(2, 2, 2))
	
	def draw(self):
		glColor3f(1., 1., 1.)
		super(SceneTest, self).draw()
		if self.game.debugMode:
			past = self.ballV + self.car.origin
			q = gluNewQuadric()
			glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) #wireframe
			gluQuadricNormals(q, GLU_SMOOTH)
			gluQuadricTexture(q, GL_TRUE)
			#Yellow ball in front of car
			glPushMatrix()
			glColor3f(.5, .5, 0)
			glTranslatef(past.x, past.y, past.z)
			gluSphere(q, .25, 10, 10)
			glPopMatrix()
	
	def step(self):
		super(SceneTest, self).step()
		
		self.car.move.rotation = self.carQ.getEulerDeg()
		#print "CarY:", self.car.move.rotation.y
		#self.carQ.normalize()
		#print "q:", self.carQ
		#print 2 * acos(clamp(self.carQ.w, -1., 1.)) * radToDeg
		print self.yAng
		
		self.ballV = self.carQ.rotateV(Vector3(0., 1., 2.5))
		#print self.carQ
		
		keys = self.game.keys
		if Key.LEFT in keys:
			self.yAng = (self.yAng + 1.) % 360.
			q = Quaternion().setEulerDeg(Vector3(0., 1., 0.))
			self.carQ = self.carQ * q
		elif Key.RIGHT in keys:
			self.yAng = (self.yAng - 1.) % 360.
			q = Quaternion().setEulerDeg(Vector3(0., -1., 0.))
			self.carQ = self.carQ * q
		if Key.Q in keys:
			q = Quaternion().setEulerDeg(Vector3(1., 0., 0.))
			self.carQ = q * self.carQ
		elif Key.A in keys:
			q = Quaternion().setEulerDeg(Vector3(-1., 0., 0.))
			self.carQ = q * self.carQ
		if Key.W in keys:
			q = Quaternion().setEulerDeg(Vector3(0., 1., 0.))
			self.carQ = q * self.carQ
		elif Key.S in keys:
			q = Quaternion().setEulerDeg(Vector3(0., -1., 0.))
			self.carQ = q * self.carQ
		if Key.E in keys:
			q = Quaternion().setEulerDeg(Vector3(0., 0., 1.))
			self.carQ = q * self.carQ
		elif Key.D in keys:
			q = Quaternion().setEulerDeg(Vector3(0., 0., -1.))
			self.carQ = q * self.carQ
		if Key.SPACE in keys:
			self.carQ = Quaternion().setEulerDeg(Vector3())
		elif Key.Z in keys:
			v = self.carQ.getEulerDeg()
			self.carQ = Quaternion().setEulerDeg(Vector3(0., v.y, 0.))
		elif Key.Z in keys:
			v = self.carQ.getEulerDeg()
			self.carQ = Quaternion().setEulerDeg(Vector3(0., v.y, 0.))
		elif Key.M in keys:
			self.ballV = self.carQ.rotateV(Vector3(0., 1., 2.5))
			print self.ballV
		elif Key.N in keys:
			#q = self.carQ.getConjugate()
			#print q
			self.carQ.x = 0.
			self.carQ.y = 1.
			self.carQ.z = 0.
			#self.carQ *= q
			
			
			